package com.picture.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.picture.constant.JwtClaimsConstant;
import com.picture.constant.MessageConstant;
import com.picture.mapper.UserMapper;
import com.picture.pojo.dto.UserRegisterDTO;
import com.picture.pojo.entity.User;
import com.picture.pojo.dto.UserLogin;
import com.picture.result.Result;
import com.picture.service.UserService;

import com.picture.utils.JwtUtil;
import com.picture.utils.MD5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.picture.properties.JwtProperties;
import java.util.HashMap;
import java.util.Map;

/**
* @author 悦神
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-02-28 23:35:30
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Autowired
    private  UserMapper userMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public Result login(UserLogin userLoginVO) {
        //根据账号查询
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,userLoginVO.getName());
        User loginUser = userMapper.selectOne(queryWrapper);

        //账号判断
        if (loginUser == null) {
            //账号错误
            throw new RuntimeException(MessageConstant.ACCOUNT_NOT_FOUND);

        }

        //判断密码
        if (!StringUtils.isEmpty(userLoginVO.getPassword())
                && loginUser.getPassword().equals(MD5Util.encrypt(userLoginVO.getPassword())))
        {
            //账号密码正确
            //根据用户唯一标识生成token

            Map<String, Object> claims = new HashMap<>();
            System.out.println("用户登录成功"+loginUser.getId());
            claims.put(JwtClaimsConstant.EMP_ID, loginUser.getId());
            String token = JwtUtil.createJWT(
                    jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(),
                    claims);


            return Result.success(token);
        }

        //密码错误
       throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
    }

    @Override
    public Result getUserInfo(String token) {
     return null;
    }

    @Override
    public Result checkUserName(String username) {
        return null;
    }

    @Override
    public Result regist(UserRegisterDTO userRegisterDTO) {
        if (!(userRegisterDTO.getValue().equals(MessageConstant.VALUE))){
            throw new RuntimeException(MessageConstant.REGISTER_EXCEPTION);
        }

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,userRegisterDTO.getName());
        Long count = userMapper.selectCount(queryWrapper);

        if (count > 0){
            throw new RuntimeException(MessageConstant.COMMON_NAME);

        }

        userRegisterDTO.setPassword(MD5Util.encrypt(userRegisterDTO.getPassword()));
        User user = new User();
        BeanUtils.copyProperties(userRegisterDTO,user);
        int rows = userMapper.insert(user);

        return Result.success();
    }
}




